<?php

namespace App\Admin\Actions\ShopProduct;

use App\Library\Logic\OperateLogLogic;
use App\Models\ShopProductModel;
use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;

class ShopProductBaseAction extends RowAction
{
    public $name = '基础信息';

    public function handle(ShopProductModel $model, Request $request): Response
    {
        $model->product_name = $request->name;
        $model->product_label = $request->label;
        $model->product_note = $request->note;
        $model->product_content = $request->detail;
        $model->product_protect = $request->protect;
        $model->product_wild_care = $request->wildCare;
        $model->product_stage = $request->stage;
        $model->product_gene = $request->gene;
        $model->product_pet_code = $request->petCode;
        $model->product_tool_species = $request->toolSpecies;
        $model->product_tool_stage = $request->toolStage;
        $model->operated_at = now();
        $model->save();
        OperateLogLogic::add(Admin::user()->user_id, 'admin.product.baseAction');
        return $this->response()->success('操作成功')->refresh();
    }

    public function form(ShopProductModel $model): void
    {
        $this->text('name', '名称')->attribute('maxlength', 20)->default($model->product_name)->required();
        $this->text('label', __('l.tag'))->attribute('maxlength', 200)->default($model->product_label);
        $this->textarea('note', __('shop_product.product_note'))->default($model->product_note);
        $this->textarea('detail', __('shop_product.product_content'))->default($model->product_content);
        if ($model->product_category_id_1 === 1) {
            $this->select('protect', __('shop_product.product_protect'))->options(['否', '是'])
                ->default($model->product_protect);
            $this->select('wildCare', __('shop_product.wildCare'))->options(__('shop_product.wildCareArr'))
                ->default($model->product_wild_care);
            $this->select('stage', __('shop_product.stage'))->options(__('shop_product.stageArr'))
                ->default($model->product_stage);
            $this->text('gene', __('shop_product.gene'))->attribute('maxlength', 20)
                ->default($model->product_gene);
            $this->text('petCode', __('shop_product.product_pet_code'))->attribute('maxlength', 30)
                ->default($model->product_pet_code);
        } else {
            $this->text('toolSpecies', __('shop_product.product_tool_species'))->attribute('maxlength', 30)
                ->default($model->product_tool_species);
            $this->text('toolStage', __('shop_product.product_tool_stage'))->attribute('maxlength', 30)
                ->default($model->product_tool_stage);
        }
    }
}
